package com.jic.interaction.JDVOP.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.JDVOP.common.utils.JdVopHttpUtil;
import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.JDVOP.model.vo.request.DeletePushMessageRequest;
import com.jic.interaction.JDVOP.model.vo.request.SelectPushMessageRequest;
import com.jic.interaction.JDVOP.model.vo.response.DeletePushMessageResponse;
import com.jic.interaction.JDVOP.model.vo.response.InvoiceDetailResponse;
import com.jic.interaction.JDVOP.model.vo.response.InvoiceItemResponse;
import com.jic.interaction.JDVOP.model.vo.response.SelectPushMessageResponse;
import com.jic.interaction.JDVOP.service.PushMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Service
@Slf4j
public class PushMessageServiceImpl implements PushMessageService {

    @Value("${route.JDVOP}")
    private String url;

    @Override
    public RestResult<List<SelectPushMessageResponse>> getPushMessage(SelectPushMessageRequest selectPushMessageRequest) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        log.info("jdvop 查询发票概要，接收参数。。{}", selectPushMessageRequest.toString());
        String u = url + "api/message/get";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(selectPushMessageRequest));
        log.info("jdvop 查询发票概要，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        List<SelectPushMessageResponse> list = (List<SelectPushMessageResponse>) JSONObject.parseObject(m.get("result").toString());
        return RestResult.success(list, m.get("resultCode").toString(), m.get("resultMessage").toString());
    }

    @Override
    public RestResult<DeletePushMessageResponse> delPushMessage(DeletePushMessageRequest deletePushMessageRequest) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("jdvop 查询发票概要，接收参数。。{}", deletePushMessageRequest.toString());
        String u = url + "api/message/get";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(deletePushMessageRequest));
        log.info("jdvop 查询发票概要，返回结果。。{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString() != "true") return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        DeletePushMessageResponse deletePushMessageResponse = JSONObject.parseObject(m.get("result").toString(), DeletePushMessageResponse.class);
        return RestResult.success(deletePushMessageResponse, m.get("resultCode").toString(), m.get("resultMessage").toString());
    }
}
